"""author: wenyao
   data: 2021/5/20
   project: devopscmdb
"""

#自身不传入参数的装饰器，使用两层函数定义
#自身传入参数的装饰器，使用三层函数定义

import time
import functools

# def deco(username):
#     def cost(func):
#         #把func的元数据复制给_cost
#         #保留元数据
#         @functools.wraps(func)
#         def _cost(*args, **kwargs):
#             print(f"装饰器带参数：{username}")
#             start = time.time()
#             result = func(*args,**kwargs)
#             end = time.time()
#             print(f"{func.__name__}执行花了{end-start}s")
#             return result
#         return _cost
#     return cost


def deco(d = None,username=None):
    def cost(func):
        #把func的元数据复制给_cost
        #保留元数据
        @functools.wraps(func)
        def _cost(*args, **kwargs):
            print(f"装饰器带参数：{username}")
            start = time.time()
            result = func(*args,**kwargs)
            end = time.time()
            print(f"{func.__name__}执行花了{end-start}s")
            return result
        return _cost
    if d:
        return cost(d)
    else:
        return cost

#@deco(username = "sc")
   #cost = deco(username = "sc")
   #add = cost(add)
@deco  #--->deco(add)
def add():
    print("thi is add")

add()